/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 * Provide operations on DefaultTableModel
 * @author: jin Chen 
 * @date: Dec 6, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.gmatrix.toolkit;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.Arrays;

public class TableModelSupport {
    DefaultTableModel tablemodel;
    JComponent component;// the component contain the tablemodel

    public TableModelSupport(JComponent component,DefaultTableModel tablemodel) {
        this.component=component;
        this.tablemodel = tablemodel;
    }

    /**************************************************
     *                operate on rows
     ***************************************************/
    public void moveTop(int row) {
        ((DefaultTableModel)this.tablemodel).moveRow(row,row,0);
        component.repaint();
    }
    public void moveTop(int[] rows) {
        Arrays.sort(rows);
        for (int i=0;i<rows.length ;i++){
            moveTop(rows[i]);
        }
        component.repaint();

    }
    public void moveBottom(int row) {
        int rowNumber=tablemodel.getRowCount();
        ((DefaultTableModel)this.tablemodel) .moveRow(row,row,rowNumber-1);
        component.repaint();
    }
    public void moveBottom(int[] rows) {
        Arrays.sort(rows);
        for (int i=rows.length -1;i>=0;i--){
            moveBottom(rows[i]);
        }
        component.repaint();
    }
    /**
     * Move a record up one row. The moving is actually a physical movement on dataModel
     * Assume the table hold a small data set, the movement won't have big performance problem
     *
     * @param row
     */
    public void moveRowUp(int row) {
         this.moveRow(row,true);
        component.repaint();
    }
    public void moveRowDown(int row) {
        this.moveRow(row,false);
        component.repaint();
    }
    /**
     * Move multiple rows up
     * @param rows  the index of the rows which is to move up
     */
    public void moveRowsUp(int[] rows) {
        Arrays.sort(rows);
        // 
        // 
        for (int i=0;i<rows.length ;i++){
            this.moveRowUp(rows[i]);
        }
        component.repaint();
    }
    public void moveRowsDown(int[] rows) {
        Arrays.sort(rows);
        //For down, move larger index first, then move smaller index
        for (int i=rows.length-1;i>=0 ;i--){
            this.moveRowDown(rows[i]);
        }
       component.repaint();
    }
    /**
     *
     * @param row
     * @param up
     */
    public  void moveRow(int row,boolean up) {
        if(up&&row<=0) return;//unable to move up anymore
        if(!up&&(row<0||row>=this.tablemodel.getRowCount()-1 )){
            //this.getRowCount()-1 is last row, can't be moved down
            return;//unable to move down anymore
        }

        if(up){
            tablemodel.moveRow(row,row,row-1);
        }
        else{
             tablemodel.moveRow(row,row,row+1);
        }
         component.repaint();
    }
}
